define(['jquery', 'bootstrap', 'backend', 'table', 'form','bootstrap-table-jumpto', 'template'], function ($, undefined, Backend, Table, Form, showJumpto,Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'rate/company/index' + location.search,
                    add_url: 'rate/company/add',
                    edit_url: 'rate/company/edit',
                    del_url: 'rate/company/del',
                    multi_url: 'rate/company/multi',
                    import_url: 'rate/company/import',
                    table: 'company',
                }
            });

            var table = $("#table");
            Fast.config.openArea = ['100%','90%'];

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showJumpto: true,
                searchFormVisible: true,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'rate_area_id', title: '税源地', addClass: "selectpage", extend: "data-source='rate/area/index'  data-field='name' data-order-by='createtime desc'" , visible:false},

                        {field: 'rate.name', title: '税源地名称', operate: false},
                        {field: 'company_id', title: '企业名称', addClass: "selectpage", extend: "data-source='rate/company/index'  data-field='name' data-order-by='createtime desc'" , visible:false},
                        {field: 'concat_phone', title: __('Concat_phone'), operate: false},
                        {field: 'code', title: '企业编号', operate: 'LIKE'},
                        {field: 'credit_code', title: '企业信用代码', operate: 'LIKE'},
                        {field: 'limit_money', title: '企业月限额', operate: false},
                        {field: 'max_tip', title: '大额提醒金额', operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit_admin',
                                    title: '修改企业账号密码',
                                    text: '修改账号',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-address-book',
                                    url:function(row){
                                        return 'rate/company/editAdmin?ids=' + row.id;
                                    }
                                },
                            ]

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'merchant/company/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'merchant/company/restore',
                                    refresh: true
                                },

                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'merchant/company/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            var newData = [];
            $('.rate-click').on('click', '.rate-remove', function () {
                var index = $(this).data('index');
                console.log(index);
                //删除元素
                newData.splice(index,1);
                //移除当前 元素
                $(this).parent().parent().remove();
            });
            function createHtml(checkedIds) {
                newData = [];
                $.each(checkedIds, function (i, item) {
                    var temp = item.split('_');
                    temp = {
                        'id': temp[0],
                        'name' : temp[1]
                    };
                    newData.push(temp);
                });

                //去除重复 重新渲染

                if (newData.length > 0) {
                    var c_html = Template('rate-c',{newData: newData})
                    $('#rate-table').remove();
                    $('#rate_content').append(c_html);
                } else {
                    $('#rate-table').remove();
                }
            }
            $('.add-region').click(function () {
                $.each(Config.rate_area, function (i, item) {
                    var flag = false;

                    $.each(newData, function (i, v) {
                        if (item.id == v.id) {
                            flag = true;
                        }
                    });
                    Config.rate_area[i].checked = flag;
                });
                        //获取
                var template_html = Template('rate-area',{rate_area: Config.rate_area})
                layer.open({
                    type: 1,
                    shade: false,
                    title: '选择税源地',
                    btn: ['确定', '取消'],
                    area: ['500px', '420px'], //宽高
                    content: template_html,
                    yes: function (index) {
                        var checkedIds = [];
                        $('input[type=checkbox][name="rate_area_ids[]"]:checked').each(function (index, item) {
                            checkedIds.push(item.value);
                        });

                        //非对立商户只能选择一个税源地row[is_has_spread]
                        var spread = $('input[type=radio][name="row[is_has_spread]"]:checked').val();
                        console.log(spread);
                        if (spread == 0) {
                            if (checkedIds.length > 1) {
                                Toastr.error('普通商户只能选择一个税源地');
                                checkedIds = [];
                                return;
                            }
                        }
                        //生成 税务资料内容
                        createHtml(checkedIds);
                        //生成html
                        layer.close(index);
                    },
                    end: function () {

                    }
                });
            })
        },
        editadmin: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            var newData = Config.rate_c;
            this.initRate(newData);
            $('.rate-click').on('click', '.rate-remove', function () {
                var index = $(this).data('index');
                //删除元素
                newData.splice(index,1);
                //移除当前 元素
                $(this).parent().parent().remove();
            });
            function createHtml(checkedIds) {
                //选择区域之后 判断newdata中是否存在
                $.each(checkedIds, function (i, item) {
                    var temp = item.split('_');
                    var flag = false;
                    $.each(newData, function (i, v) {
                        if (temp[0] == v.rate_area_id) {
                            flag = true;
                        }
                    });

                    temp = {
                        'rate_area_id': temp[0],
                        'name' : temp[1],
                        'year_tip' : '',
                        'month_tip' : '',
                        'limit_money' : '',
                        'evaluation_expression' : '',
                        'is_check' : 0,
                    };
                    if (!flag) {
                        newData.push(temp);
                    }
                });

                //去除重复 重新渲染

                if (newData.length > 0) {
                    var c_html = Template('rate-c',{newData: newData})
                    $('#rate-table').remove();
                    $('#rate_content').append(c_html);
                } else {
                    $('#rate-table').remove();
                }
            }
            $('.add-region').click(function () {
                $.each(Config.rate_area, function (i, item) {
                    var flag = false;

                    $.each(newData, function (i, v) {
                        if (item.id == v.rate_area_id) {
                            flag = true;
                        }
                    });
                    Config.rate_area[i].checked = flag;
                });
                //获取
                var template_html = Template('rate-area',{rate_area: Config.rate_area})
                layer.open({
                    type: 1,
                    shade: false,
                    title: '选择税源地',
                    btn: ['确定', '取消'],
                    area: ['800px', '620px'], //宽高
                    content: template_html,
                    yes: function (index) {
                        var checkedIds = [];
                        $('input[type=checkbox][name="rate_area_ids[]"]:checked').each(function (index, item) {
                            checkedIds.push(item.value);
                        });

                        //非对立商户只能选择一个税源地row[is_has_spread]
                        var spread = $('input[type=radio][name="row[is_has_spread]"]:checked').val();
                        if (spread == 0) {
                            if (checkedIds.length > 1) {
                                Toastr.error('普通商户只能选择一个税源地');
                                checkedIds = [];
                                return;
                            }
                        }
                        //生成 税务资料内容
                        createHtml(checkedIds);
                        //生成html
                        layer.close(index);
                    },
                    end: function () {

                    }
                });
            })
        },
        initRate: function(newData) {
            var c_html = Template('rate-c',{newData: newData})
            $('#rate-table').remove();
            $('#rate_content').append(c_html);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $('#fachoose-express', $("form[role=form]")).click(function () {
                    var url = Fast.api.fixurl('rate/company/rateDetail')
                    Fast.api.open(url,'选择服务费计算公式', );
                });
            }
        }
    };
    return Controller;
});